package com.lanyou.esb.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.userManagement.User;
import com.lanyou.cook.service.security.UserSessionService;
import com.lanyou.esb.datadictionary.AlarmLevel;
import com.lanyou.esb.datadictionary.AlarmType;
import com.lanyou.esb.entity.IfAlarmHis;
import com.lanyou.esb.repository.IfAlarmRepository;
import com.lanyou.esb.web.param.IfAlarmHisParam;

@RestController
@RequestMapping(Constants.BASE_URL_API + "/ifAlarmHises")
public class IfAlarmHisController{
	
	@Autowired
	private IfAlarmRepository ifAlarmRepository;
	@Autowired
	private UserSessionService userSessionService;
	
	@RequestMapping(value = "/search/findByExamples", produces = "application/json")
	public Page<IfAlarmHis> ifAlaramHis(IfAlarmHisParam param, Pageable pageable) {
		
		if(param.getAlarmLevel()==null){
			param.setAlarmLevel("");
		}
		if(param.getAlarmType()==null){
			param.setAlarmType("");
		}
			
		if(param.getFieldName()==null){
			param.setFieldName("");
		}
		if(param.getInterfaceName()==null){
			param.setInterfaceName("");
		}else{
			param.setInterfaceName("%"+param.getInterfaceName()+"%");	
		}
		if(param.getSystemName()==null){
			param.setSystemName("");
		}
		
		if (param.getBegainTime() == null) {
			param.setBegainTime("");
		} 
		if (param.getEndTime() == null) {
			param.setEndTime("");
		}
		int page1, page2;
		User user = userSessionService.getCurrentAuthenticatedUser();
		String userId = "";
		if(user!=null){
			 userId = user.getId();
		}
		page1 = pageable.getPageNumber()*pageable.getPageSize();
		page2 = pageable.getPageSize();

		List<Object[]> ifAlarmHis = ifAlarmRepository.findAlarmHis(param.getInterfaceName(),
				param.getFieldName(), 
				param.getSystemName(), 
				param.getAlarmLevel(), 
				param.getAlarmType(), 
				param.getBegainTime(),
				param.getEndTime(),
				userId,
				page1,
				page2);
				
		List<Object[]> totals = ifAlarmRepository.countAlarmHis(param.getInterfaceName(),
				param.getFieldName(), 
				param.getSystemName(), 
				param.getAlarmLevel(), 
				param.getAlarmType(), 
				param.getBegainTime(),
				param.getEndTime(),
				userId);
		List<IfAlarmHis> result = new ArrayList<IfAlarmHis>(totals.size());
		int total = totals.size();
		for (Object[] ifAlarmhis : ifAlarmHis) {
			
			IfAlarmHis ifAlarmhi = new IfAlarmHis();
			ifAlarmhi.setFieldname(ifAlarmhis[0] != null ? (String) ifAlarmhis[0]: "");
			ifAlarmhi.setSystemName(ifAlarmhis[1] != null ? (String) ifAlarmhis[1]: "");
			ifAlarmhi.setFieldId(ifAlarmhis[2] != null ? (String) ifAlarmhis[2]: "");
			ifAlarmhi.setSystemId(ifAlarmhis[3] != null ? (String) ifAlarmhis[3]: "");
			ifAlarmhi.setIfName(ifAlarmhis[4] != null ? (String) ifAlarmhis[4]: "");
			if(StringUtils.equals(ifAlarmhis[5].toString(),AlarmLevel.GENERAL.ValueCode)){
				ifAlarmhi.setAlarmLevel("一般告警");
			}
			if(StringUtils.equals(ifAlarmhis[5].toString(),AlarmLevel.IMPORTANCE.ValueCode)){
				ifAlarmhi.setAlarmLevel("重要告警");
			}
			if(StringUtils.equals(ifAlarmhis[5].toString(),AlarmLevel.URGENT.ValueCode)){
				ifAlarmhi.setAlarmLevel("紧急告警");
			}
			if(StringUtils.equals(ifAlarmhis[6].toString(),AlarmType.HTTP_400.ValueCode)){
				ifAlarmhi.setAlarmType("HTTP400c错误");
			}
			if(StringUtils.equals(ifAlarmhis[6].toString(),AlarmType.REQUEST_OVER_LIMIT.ValueCode)){
				ifAlarmhi.setAlarmType("请求超限");
			}
			if(StringUtils.equals(ifAlarmhis[6].toString(),AlarmType.REQUEST_TIMEOUT.ValueCode)){
				ifAlarmhi.setAlarmType("请求超时");
			}
			ifAlarmhi.setErrorDate(ifAlarmhis[7] != null ? (Date) ifAlarmhis[7]: null);
			
			ifAlarmhi.setDealTime(ifAlarmhis[8] != null ? (Date) ifAlarmhis[8]: null);
			ifAlarmhi.setDealPerson(ifAlarmhis[9] != null ? (String) ifAlarmhis[9]: "");
			ifAlarmhi.setAlarmResult(ifAlarmhis[10] != null ? (String) ifAlarmhis[10]: "");
			result.add(ifAlarmhi);
		}
		Page<IfAlarmHis> page3 = new PageImpl<IfAlarmHis>(result, pageable, total);
		return page3;
	}

}